﻿using DevExpress.XtraEditors;
using MesProjectWinform.Equipment.Output;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MesProjectWinform.Equipment.Form.DeviceType
{
    /// <summary>
    /// 设备类型添加页面
    /// </summary>
    public partial class DeviceTypeAddForm : DevExpress.XtraEditors.XtraForm
    {
        public DeviceTypeAddForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 添加设备类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void AddDeviceTypeButton1_Click(object sender, EventArgs e)
        {
            DeviceTypeOuput deviceTypeOutput = new DeviceTypeOuput
            {
                DeviceTypeName = DeviceTypeName.Text,
                DeviceTypeDescription = DeviceTypeDescription.Text,
            };
            using (var client = new HttpClient())
            {
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(deviceTypeOutput);
                var content = new System.Net.Http.StringContent(json, Encoding.UTF8, "application/json");
                var response = await client.PostAsync("http://47.110.243.157:5229/api/DeviceType/AddDeviceType", content);
                // 检查响应是否成功
                if (response.IsSuccessStatusCode)
                {
                    //如果添加成功，则关闭当前窗口，跳转到设备类型列表页面
                   
                    // 如果成功，显示提示信息
                    XtraMessageBox.Show("设备类型添加成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.DialogResult=DialogResult.OK; // 设置对话框结果为OK
                    //关闭当前窗体
                    this.Close();
                }
                else
                {
                    // 如果失败，显示错误信息
                    XtraMessageBox.Show("设备类型添加失败，请稍后重试。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}